//
//  RegisterViewController.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/23.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegistView.h"
#import "LoginViewController.h"
#import "AVOSCloud/AVOSCloud.h"


@interface RegisterViewController ()
@property(nonatomic,strong) RegistView *registView;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
   
    [self steupRegist];
}



-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)steupRegist{
    self.registView = [[RegistView alloc] initWithFrame:self.view.bounds];
    self.registView.backgroundColor = [UIColor whiteColor];
    [self.registView.registBtn addTarget:self action:@selector(registBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.registView.cancelBtn addTarget:self action:@selector(quxiaotBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registView];
    
}



-(void)registBtn:(UIButton *)button{
    
    AVQuery *query = [AVQuery queryWithClassName:@"MyLoadPractice"];
    [query selectKeys:@[@"userName"]];
    NSArray *result = [query findObjects];
    for (NSDictionary *dic in result) {
        NSString *userName = dic[@"userName"];
            if ([_registView.userTextField.text isEqualToString:userName]){
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"此用户名已经被注册,请换其他用户名重试" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *defineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
            [alertC addAction:cancelAction];
            [alertC addAction:defineAction];
            [self.navigationController presentViewController:alertC animated:YES completion:nil];
                return;
                
        }
    }
    
            if (![_registView.countersignPasswordTextField.text isEqualToString:_registView.passwordTextField.text]) {
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"两次输入密码不一致,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                UIAlertAction *defineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
                [alertC addAction:cancelAction];
                [alertC addAction:defineAction];
                [self presentViewController:alertC animated:YES completion:nil];
            }else if (_registView.userTextField.text == nil || [_registView.userTextField.text isEqualToString:@""]||[_registView.userTextField.text isEqualToString:@" "]||[_registView.userTextField.text isEqualToString:@"  "]||[_registView.userTextField.text isEqualToString:@"   "]||[_registView.userTextField.text isEqualToString:@"    "]||[_registView.userTextField.text isEqualToString:@"     "]){
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                UIAlertAction *defineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
                [alertC addAction:cancelAction];
                [alertC addAction:defineAction];
                [self presentViewController:alertC animated:YES completion:nil];

            }else if (_registView.passwordTextField.text == nil ||[_registView.passwordTextField.text isEqualToString:@""]||[_registView.passwordTextField.text isEqualToString:@" "]||[_registView.passwordTextField.text isEqualToString:@"  "]||[_registView.passwordTextField.text isEqualToString:@"   "]||[_registView.passwordTextField.text isEqualToString:@"    "]||[_registView.passwordTextField.text isEqualToString:@"     "]){
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                UIAlertAction *defineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
                [alertC addAction:cancelAction];
                [alertC addAction:defineAction];
                [self presentViewController:alertC animated:YES completion:nil];
            }else{
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"恭喜您,注册成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *defineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                AVObject *post = [AVObject objectWithClassName:@"MyLoadPractice"];
                [post setObject:self.registView.userTextField.text forKey:@"userName"];
                NSLog(@"%@",self.registView.userTextField.text);
                [post setObject:self.registView.passwordTextField.text forKey:@"passWord"];
                [post save];
                
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertC addAction:cancelAction];
            [alertC addAction:defineAction];
            [self.navigationController presentViewController:alertC animated:YES completion:nil];
            }
    
    }
-(void)quxiaotBtn:(UIButton *)button{
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"你将要取消注册,退回登陆界面" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel= [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction * du = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alter addAction:cancel];
    [alter addAction:du];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

@end
